package com.zhentao.service.impl;

import com.zhentao.model.common.app.dto.FriendDeleteDto;
import com.zhentao.model.common.app.dto.FriendFindAllDto;
import com.zhentao.model.common.app.pojo.Friend;
import com.zhentao.model.common.app.vo.FriendAndUserIVO;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.ApFriendService;
import com.zhentao.mapper.ApFriendMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 薄超轩
* @description 针对表【friend】的数据库操作Service实现
* @createDate 2024-10-06 21:40:02
*/
@Service
public class ApFriendServiceImpl implements ApFriendService {
@Autowired
private ApFriendMapper apFriendMapper;



    @Override
    public ResponseResult findAll(FriendFindAllDto dto) {
        List<FriendAndUserIVO>friends=apFriendMapper.findAll(dto);
        return ResponseResult.okResult(friends);
    }


    /**
     * app端：交友圈发布
     * @param friend
     * @return
     */
    @Override
    public ResponseResult friendSave(Friend friend) {
        if (friend!=null){
            System.out.println(friend.getImages());
            String[] images = friend.getImages();
            friend.setImage(StringUtils.join(images,","));
            friend.setCreateTime(new Date());
            apFriendMapper.insert(friend);
            return ResponseResult.okResult("发布成功");
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }


    /**
     * app端：删除朋友圈操作
     * @param dto
     * @return
     */
    @Override
    public ResponseResult deleteFriend(FriendDeleteDto dto) {
        if (dto!=null){
            Friend friend = apFriendMapper.selectById(dto.getId());
            System.out.println(friend+"----------------------");
            if (friend.getUserId()==(dto.getUserId())){
                apFriendMapper.deleteById(dto.getId());
                return ResponseResult.okResult("删除成功");
            }else {
                return ResponseResult.errorResult(808, "你只能删除你的作品");
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }
}




